import BaseService from "./BaseService.js";
const app = getApp();
const util = require('../util');

module.exports = {
  //断电
  instructSendOff:function (data,port) {
    return BaseService.common("instruct"+port+"/instructSendOff", data);
  },

  //通电
  instructSendNo:function (data,port) {
    return BaseService.common("instruct"+port+"/instructSendNo", data);
  },

  //物联网设备检测
  selfCheck:function (data) {
    return BaseService.common("selfCheck/selfCheck", data);
  },

  //添加工牌人员
  bindCard:function (data) {
    return BaseService.common("personnel/bindCard", data);
  },

  //电子工牌查询工牌的详情
  findPersonnel:function(data){
    return BaseService.go("electronCard/findPersonnel", data);
  },

  //日志埋点
  log:function (data) {
    return BaseService.common("log/save", data);
  },

  //查询
  findList: function (data,otherDataAuth,isConnect) {    
    let currentUser=app.globalData.userInfo;
    if(currentUser){
      if(isConnect){
        data.userDataAuthority = currentUser.dataAuthorityOfUser;
        currentUser.dataAuthorityOfUser.depart=currentUser.installDepart;
        util.uniqueConcat(data.userDataAuthority.depart,otherDataAuth);
      }else{
        let isAll = false;

        //查询条件中不验证权限
        if(data.checkDataAuthority==false){
          isAll = true;
        }else{
          currentUser.roles.forEach(role => {        
            if(role.appId.indexOf(3)!=-1 && role.dataAuthOfRole.key == 'all') {
              isAll = true;
            }
          })
        }  

        if(isAll){
          data.checkDataAuthority = false;
        }else{
          data.userDataAuthority = currentUser.dataAuthorityOfUser;   
          if (otherDataAuth) {          
            util.uniqueConcat(data.userDataAuthority.depart,otherDataAuth);         
          }  
        }
      }
      
      let checkState= {
        "fieldName": "validState",
        "relation": "!=",
        "value": 0
      };
      if(!data.condition){
        data.condition=[];
      }
      data.condition.push(
        checkState
      );      
    }    
    return BaseService.nb("findList", data);
  },

  //查询异常数据
  findErrorDataListGo:function(data){
    return BaseService.go("errorData/findList", data);
  },

  //查询异常数据-内部人员
  findErrorDataInsideListGo:function(data){
    return BaseService.go("errorData/findListInside", data);
  },

  //作废异常数据
  deleteErrorDataListGo:function(data){
    return BaseService.go("errorData/deleteErrorData", data);
  },

  //添加/修改
  save:function (data) {
    return BaseService.nb("save", data);
  },

  //修改单一字段
  update:function (data) {
    return BaseService.nb("update", data);
  },

  //删除
  delete:function (data) {
    return BaseService.nb("delete", data);
  },

  //获取物联网信息
  findOneByImei:function (data) {
    return BaseService.equipment("findOneByImei", data);
  },

  //旧模块的指令
  sendcmd:function (data) {
    return BaseService.equipment("sendcmd", data);
  },

  //分组查询
  groupFindList: function (data,otherDataAuth) {
    let currentUser = app.globalData.userInfo;
    if (currentUser) {  
      let isAll = false;
      currentUser.roles.forEach(role => {        
        if (role.appId.indexOf(3)!=-1 && role.dataAuthOfRole.key == 'all') {
          isAll = true;
        }        
      })

      if (isAll) {
        data.checkDataAuthority = false;
      } else {
        data.userDataAuthority = currentUser.dataAuthorityOfUser;
        if (otherDataAuth) {
          util.uniqueConcat(data.userDataAuthority.depart, otherDataAuth);
        }
      }
      let checkState = {
        "fieldName": "validState",
        "relation": "!=",
        "value": 0
      };
      if (!data.condition) {
        data.condition = [];
      }
      data.condition.push(
        checkState
      );

      if(data.checkDataAuthority==false){
        data.checkDataAuthority = false;
      }
    }
    return BaseService.nb("group/findList", data);
  },

  //获取用户信息
  getUserByName:function (data) {
    return BaseService.authority("user/getUserByName", data);
  },

  //获取用户信息（用于验证码登录前的判断，不进行token验证）
  getUserName:function (data) {
    return BaseService.authority2("user/getUserName", data);
  },

  //获取个人中心菜单
  getMenu:function (data) {
    return BaseService.authority("menu/findList", data);
  },

  //发送验证码
  sendSms:function (data) {
    return BaseService.wechat2("sendSms/send", data);
  },

  //查询验证码
  querySms:function (data) {
    return BaseService.wechat2("sendSms/querySms", data);
  },

  //公众号授权
  wechatLogin:function (data) {
    return BaseService.wechat("login/wechatLogin", data);
  },

  //添加报修
  repairAdd:function (data) {
    return BaseService.equipment2("repair/saveOrUpdate", data);
  }, 

  //公众号发送维修提醒
  sendRepairTiXing:function (data) {
    return BaseService.wechat2("sendRepairTiXing/sendRepairTiXing", data);
  },

  //查询设备列表
  equipmentList:function (data) {
    return BaseService.equipment("findList", data);
  },

  //绑定kafka分区 
  bindPartition:function (data) {
    return BaseService.common("partition/bindPartition", data);
  },

  //保存日志
  saveLog:function (business,operateType,dataId,className,data) {
    let obj={
      //登录用户ID
      userId:app.globalData.userInfo.id,
      //应用ID
      appId:app.globalData.myAppId, 
      //业务页面
      business:business,    
      //操作
      operateType:operateType
    };
    if(dataId){
      //操作的数据id
      obj.dataId=dataId;
    }
    if(className){
      //类名
      obj.className=className;
    }
    if(data){
      //操作的具体数据
      obj.data=data;
    }
    this.log(obj);
  },

  //外电补偿
  powerCompensate:function (data,port) {
    return BaseService.common("instruct"+port+"/powerCompensate", data);
  },
  //设置震动模式
  setVibration:function (data,port) {
    return BaseService.common("instruct"+port+"/setVibration", data);
  },
  //设置硬件模式
  recoveryInit:function (data,port) {
    return BaseService.common("instruct"+port+"/recoveryInit", data);
  },
  //指令-设置4G模块开启外电检测功能
  openPowerCheck:function (data,port) {
    return BaseService.common("instruct"+port+"/openPowerCheck", data);
  },
  //指令-打开静止过滤
  staticFilterON:function (data,port) {
    return BaseService.common("instruct"+port+"/staticFilterON", data);
  },
  //指令-关闭静止过滤
  staticFilterOFF:function (data,port) {
    return BaseService.common("instruct"+port+"/staticFilterOFF", data);
  },  
  
  //完成售后记录
  afterSaleOver:function (data) {
    return BaseService.equipment("afterSale/afterSaleOver", data);
  },

  //月度报表-类别统计
  runInformation:function (data) {
    return BaseService.report("equipment/runInformation", data);
  },

  //月度报表-数据分析图表
  usageInformation:function (data) {
    return BaseService.report("equipment/usageInformation", data);
  },

  //发送围栏指令  
  setElectronicsFence:function (data) {
    return BaseService.common("instruct8882/setElectronicsFence", data);
  },

  //查询电子工牌轨迹
  selectTrajectory:function (data) {
    return BaseService.common("personnel/selectTrajectory", data);
  },
 
  //考勤统计-个人-日统计
  dayPersonal:function (data) {
    return BaseService.common("personnel/selectAttendanceStatisticsDayPersonal", data);
  },

  //考勤统计-个人-月统计
  monthPersonal:function (data) {
    return BaseService.common("personnel/selectAttendanceStatisticsMonthPersonal", data);
  },
  
  //考勤统计-团队-日统计  
  dayTeam:function (data) {
    return BaseService.common("personnel/selectAttendanceStatisticsDayTeam", data);
  },

  //考勤统计-团队-月统计  
  monthTeam:function (data) {
    return BaseService.common("personnel/selectAttendanceStatisticsMonthTeam", data);
  },
  
  //考勤统计-各部门出勤率
  attendanceRate:function (data) {
    return BaseService.common("personnel/selectDepartAttendanceRate", data);
  },

  //报表统计-周排行
  recordOfWeek:function (data) {
    return BaseService.report("equipment/getClientEquipmentRunDataRecordOfWeekInWeChat", data);
  },

  //报表统计-周统计
  reportByWeek:function (data) {
    return BaseService.go("chart/weeklyStatistics", data);
  },

  //指令-设备进场
  deleteIotRunData:function (data) {
    return BaseService.go("dataOpr/deleteIotRunData", data);
  },  

  //go项目-查询设备列表
  goFindEquipmentList: function (data,isAuth) {
    //是否按权限查询
    let isAuthFlag=true;
    if(isAuth===false){
      isAuthFlag=false;
    }

    let currentUser=app.globalData.userInfo;
    if(currentUser){
      let dataAuthOfRoles=currentUser.roles;    
      let isAll=false;
      dataAuthOfRoles.forEach(role=>{
        for(let key in role.dataAuthOfRole){
          if(role.dataAuthOfRole[key]=='all'){
            isAll=true;
          }
        }
      })
      if(currentUser && (!isAll) && isAuthFlag){
        data.departAuth=currentUser.dataAuthorityOfUser.depart;
      }
    }
    return BaseService.go("equipment/findList", data);
  },

  //go项目-维修提醒
  sendEquipmentRepair:function (data){
    return BaseService.go("wechat/sendEquipmentRepair", data);
  },

  //go项目-通用查询
  goFindList:function (param){
    /*参数传入一个对象，key固定
      {
        isAuth:是否按权限查询,
        className:查哪个表,
        data:{}查询的字段，也就是实际参数
      }
    */
   
    if(param.isAuth===true){
      //获取用户权限
      let currentUser=app.globalData.userInfo;
      if(currentUser){
        let dataAuthOfRoles=currentUser.roles;    
        let isAll=false;
        dataAuthOfRoles.forEach(role=>{
          for(let key in role.dataAuthOfRole){
            if(role.dataAuthOfRole[key]=='all'){
              isAll=true;
            }
          }
        })
        if(currentUser && (!isAll)){
          param.data.departAuth=currentUser.dataAuthorityOfUser.depart;
        }
      }
    }

    //日志（传入页面路径）
    let pages = getCurrentPages();
    let lastPage = pages[pages.length - 1];
    param.data.function = lastPage.route;
    
    return BaseService.goNb(param.className+"/findList", param.data);
  },

  //go项目-通用保存
  goSave:function (param){
    // 传参说明:
    // let param={
    //   className:表名（名称是后台配置文件写好的）      
    //   data:{}字段
    // }

    //日志（传入页面路径）
    let pages = getCurrentPages();
    let lastPage = pages[pages.length - 1];
    param.data.function = lastPage.route;

    return BaseService.goNb(param.className+"/save", param.data);
  },

  //go项目-数据检测
  goCheckRepeat:function (param){
    return BaseService.goNb(param.className+"/checkRepeat", param.data);
  },

  //go项目-关联查询字段更新到设备表
  updateEquipmentAll:function (data){
    return BaseService.go("dataOpr/updateEquipmentAll", data);
  },

  mongoToElastic:function(data){
    return BaseService.go("dataOpr/mongoToElastic", data);
  },

  deleteToElastic:function(data){
    return BaseService.go("dataOpr/deleteEs", data);
  },


  //go项目-设备分组
  getGroup:function (data){
    return BaseService.go("ui/equipmentFast/getGroup", data);
  },

  //go项目-个人中心-简报看板
  personCenterCounts:function (data){
    return BaseService.go("ui/personCenterCounts",data);
  },

  //go项目-删除
  goDelete:function (param){
    return BaseService.goNb(param.className+"/delete", param.data);
  },

  //go项目-更新
  goUpdate:function (param){
    return BaseService.goNb(param.className+"/update", param.data);
  },

  //go项目-查询物联网信息-单台设备
  goFindOneByImei:function (param){
    return BaseService.go("equipment/findOneByImei", param);
  },

  //go项目-物联网数据运行统计
  getRunTimeByDateRange:function (data){
    return BaseService.go("report/getRunTimeByDateRange", data);
  },

  //go项目-物联网日志
  iotDatalog:function (param){
    return BaseService.go("log/iotDatalog", param);
  },
  
  //查询异常数据阈值
  getThreshold:function (param){
    return BaseService.go("equipment/getRunTimeThreshold", param);
  },

  //设置异常数据阈值
  setThreshold:function (param){
    return BaseService.go("equipment/setRunTimeThreshold", param);
  },
  
  //设置设备围栏
  setFence:function (param){
    return BaseService.go("equipment/setFence", param);
  },

  //获取设备分类
  getCategoryTree(){
    return BaseService.go("ui/equipmentCategory/getTree",{});
  },

  //报修接待-新
  maintenanceReception(param){
    return BaseService.go("es/afterSaleExport/maintenanceReception", param);
  },

  //获取轨迹
  trajectory:function(data){
    return BaseService.go("chart/trajectory", data);
  },

  //个人中心-每日工作量
  findCostData:function(data){
    let currentUser=app.globalData.userInfo;
    if(currentUser){
      data.departAuth=currentUser.dataAuthorityOfUser.depart;
    }
    return BaseService.go("chart/findCostData", data);
  },

  //个人中心-设备出勤率
  findAttendanceRate:function(data){
    let currentUser=app.globalData.userInfo;
    if(currentUser){
      data.departAuth=currentUser.dataAuthorityOfUser.depart;
    }
    return BaseService.go("chart/findAttendanceRate", data);
  },

  //个人中心-平均时长
  avgRuntime:function(data){
    return BaseService.go("report/allEquipmentEachDayAvgRuntime", data);
  },

  //调用用友接口，自动生成采购入库单
  yyInterfaces:function(data){
    return BaseService.yy("bills/eInStorage", data);
  },

  //报修-2022-8新改
  addRepair:function(data){
    return BaseService.go("es/afterSaleRepair/repairs", data);
  },

  //维修转耗材
  changeToConsumable:function(data){
    return BaseService.go("es/afterSaleRepair/m2c", data);
  },

  //耗材转维修
  changeToMaintain:function(data){
    return BaseService.go("es/afterSaleRepair/c2m", data);
  },

  //维修闭单
  closeMaintainOrder:function(data){
    return BaseService.go("es/afterSaleRepair/closeMaintainOrder", data);
  },

  //删除维修工单
  delMaintainOrder:function(data){
    return BaseService.go("es/afterSaleRepair/delMaintainOrder", data);
  },

  //设备发货生成配件采购单
  automaticSaveDeliveryPartsRecord:function(data){
    return BaseService.go("deliveryParts/automaticSaveDeliveryPartsRecord", data);
  },

  //更新t_purchase_equipment表
  updatePurchaseEquipment:function(data){
    return BaseService.go("updateOrder/updatePurchaseEquipment", data);
  },
  
  //保存时同步pcsql部门表
  syncDepart:function (param){
    return BaseService.syncPgSql("syncDepart", param);
  },

  //保存时同步pcsql设备表
  syncEquipment:function (param){
    return BaseService.syncPgSql("syncEquipment", param);
  },

  //删除时同步pcsql设备表
  deleteEquipment:function (param){
    return BaseService.syncPgSql("deleteEquipment", param);
  },

  //删除时同步pcsql部门表
  deleteDepart:function (param){
    return BaseService.syncPgSql("deleteDepart", param);
  },

  //指令-设置上数频率
  generalInstruction:function (data,port) {
    return BaseService.common("instruct"+port+"/generalInstruction", data);
  },
  
}